package com.zq.it.action.exam;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.zq.it.action.BaseAction;
import com.zq.it.dao.common.PageConfig;
import com.zq.it.pojo.Student;
import com.zq.it.pojo.TClass;
import com.zq.it.pojo.Teacher;
import com.zq.it.pojo.exam.TClassTask;
import com.zq.it.pojo.exam.TStudentTask;
import com.zq.it.pojo.exam.TTask;
import com.zq.it.pojo.exam.TTaskmodel;
import com.zq.it.pojo.exam.data.ObjectData;
import com.zq.it.tool.Constant;
import com.zq.it.tool.StringUtil;
import com.zq.it.tool.UUid;

public class TaskAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Teacher teacher;
	private ObjectData tasks;
	private ObjectData taskModels;
	private TTask task;
	private TTaskmodel taskModel;
	private File demoteModelFile;
	private String demoteModelFileFileName;
	private File anwserModelFile;
	private String anwserModelFileFileName;
	private Long[] ids;
	private int[] classIds;
	private String startTime;
	private String endTime;
	private Integer taskType; // 1:普通模板、2：降级模板、3答案模板
	private ObjectData tclasses; // 分配任务的班级
	private ObjectData stuTaskList; // 已分配任务列表
	private TClass tclass;
	private Student student;
	private TStudentTask stuTask;
	private List classList;
	private ObjectData studentList;
	private TClassTask classTask;

	/**
	 * 获取任务模板列表
	 * 
	 * @return
	 */
	
	public String taskModelList() {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		PageConfig config = new PageConfig();
		config.setHql("from TTaskmodel as t where t.uploader=" + teacher.getId()
				+ "order by t.uploadTime");
		config.setCountHql(
				"select count(*) from TTaskmodel as t where t.uploader="
						+ teacher.getId());
		config.setPage(page);
		config.setPageSize(Constant.PAGE_SIZE);
		config.setUrl("task_taskModelList.asp");
		config.setPerPageCount(Constant.PER_PAGE_COUNT);
		taskModels = taskService.getListByPage(config);
		return "taskModelList";
	}

	/**
	 * 模板下载
	 * 
	 * @throws UnsupportedEncodingException
	 * 
	 */
	public void downloadModel() throws UnsupportedEncodingException {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		taskModel = taskService.getTaskModel(teacher, ids[0]);
		if (null != taskModel && null != taskType) {
			switch (taskType) {
				case 1 :
					download(
							"Model" + taskModel.getModelUrl().substring(
									taskModel.getModelUrl().lastIndexOf("."),
									taskModel.getModelUrl().length()),
							Constant.TASKMODEL + taskModel.getModelUrl());
					break;
				case 2 :
					download(
							"DemoteModel" + taskModel.getDemoteModelUrl()
									.substring(taskModel.getDemoteModelUrl()
											.lastIndexOf("."),
									taskModel.getDemoteModelUrl().length()),
							Constant.TASKMODEL + taskModel.getDemoteModelUrl());
					break;
				case 3 :
					download(
							"AnwserModel" + taskModel.getAnwserModelUrl()
									.substring(taskModel.getAnwserModelUrl()
											.lastIndexOf("."),
									taskModel.getAnwserModelUrl().length()),
							Constant.TASKMODEL + taskModel.getAnwserModelUrl());
					break;
				default :
					break;
			}
		}
	}

	/**
	 * 多条件查询任务模板
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String searchTaskModel() throws UnsupportedEncodingException {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		if (!StringUtil.isEmpty(taskModel.getModelName())) {
			taskModel.setModelName(URLDecoder.decode(taskModel.getModelName(),
					Constant.UTF_8));
		}
		if (!StringUtil.isEmpty(taskModel.getTechStyle())) {
			taskModel.setTechStyle(URLDecoder.decode(taskModel.getTechStyle(),
					Constant.UTF_8));
		}
		String hql = taskService.creatModelHql(taskModel, startTime, endTime,
				teacher);
		PageConfig config = new PageConfig();
		config.setHql(hql);
		config.setCountHql("select count(*) " + hql);
		config.setPage(page);
		config.setPageSize(Constant.PAGE_SIZE);
		config.setUrl("task!searchTaskModel.asp");
		config.setExtendUrl("taskModel.modelName="
				+ URLEncoder.encode(URLEncoder.encode(taskModel.getModelName(),
						Constant.UTF_8), Constant.UTF_8)
				+ "&taskModel.techStyle=" + taskModel.getTechStyle()
				+ "&startTime=" + startTime + "&endTime=" + endTime);
		config.setPerPageCount(Constant.PER_PAGE_COUNT);
		taskModels = taskService.getListByPage(config);
		return "taskModelList";
	}

	/**
	 * 进入增加任务模板
	 * 
	 */
	public String preAddTaskModel() {
		return "preAddTaskModel";
	}

	/**
	 * 增加任务模板
	 * 
	 * @return
	 * @throws IOException
	 * @throws Exception
	 */
	public void addTaskModel() throws IOException {
		setUploadFileName(UUid.getUUid()+ getUploadFileName().substring(getUploadFileName().lastIndexOf("."),
						getUploadFileName().length()));
		setDemoteModelFileFileName(UUid.getUUid() + demoteModelFileFileName
				.substring(demoteModelFileFileName.lastIndexOf("."),
						demoteModelFileFileName.length()));
		setAnwserModelFileFileName(UUid.getUUid() + anwserModelFileFileName
				.substring(anwserModelFileFileName.lastIndexOf("."),
						anwserModelFileFileName.length()));
		try {
			upLoad(getUpload(), getRealPath(Constant.TASKMODEL) + File.separator + getUploadFileName());
			
			uploadFile(Constant.TASKMODEL, demoteModelFileFileName,
					demoteModelFile);
			uploadFile(Constant.TASKMODEL, anwserModelFileFileName,
					anwserModelFile);
			taskModel.setAnwserModelUrl(anwserModelFileFileName);
			taskModel.setDemoteModelUrl(demoteModelFileFileName);
			taskModel.setModelUrl(getUploadFileName());
			teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
			taskModel.setUploader(teacher.getId());
			taskModel.setUploadTime(
					new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
			taskService.addTaskModel(taskModel);
			print_html_utf8("<script>parent.result('添加成功！')</script>");
		} catch (Exception e) {
			deleteFile(Constant.TASKMODEL + this.getUploadFileName());
			deleteFile(Constant.TASKMODEL + demoteModelFileFileName);
			deleteFile(Constant.TASKMODEL + anwserModelFileFileName);
			print_html_utf8("<script>parent.result('添加失败！')</script>");
		}
	}

	/**
	 * 删除任务模板 (已添加验证)
	 * 
	 * @throws IOException
	 * 
	 */
	public void deleteTaskModel() throws IOException {
		try {
			teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
			print_text_utf8(taskService.deleteTaskModel(ids,
					getRealPath(Constant.TASKMODEL), teacher));
		} catch (Exception e) {
			e.printStackTrace();
			print_text_utf8("false");
		}
	}

	/**
	 * 
	 * 读取本教师所有任务数据
	 * 
	 */
	public String getTaskList() {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		PageConfig config = new PageConfig();
		config.setHql("from TTask as t where t.teacher.id=" + teacher.getId() + " order by t.createTime");
		config.setCountHql("select count(*) from TTask as t where t.teacher.id="
				+ teacher.getId());
		config.setPage(page);
		config.setPageSize(Constant.PAGE_SIZE);
		config.setUrl("task_getTaskList.asp");
		config.setPerPageCount(Constant.PER_PAGE_COUNT);
		tasks = taskService.getListByPage(config);
		return "getTaskList";
	}

	/**
	 * 进入增加任务的页面
	 * 
	 * @return
	 */
	public String preAddTask() {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		taskModels = taskService.getTaskModelByTeacher(teacher);
		return "add";
	}

	/**
	 * 增加任务
	 * 
	 * @throws IOException
	 * 
	 */
	public void addTask() throws IOException {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		try {
			task.setTeacher(teacher);
			taskService.addTask(task);
			print_html_utf8("<script>parent.result('添加成功！')</script>");
		} catch (Exception e) {
			print_html_utf8("<script>parent.result('添加失败！')</script>");
		}
	}

	/**
	 * 显示任务详情
	 * 
	 * @return
	 */
	public String showDetail() {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		task = taskService.getTaskById(teacher, ids[0]);
		if (null != task) {
			taskModel = taskService.getTaskModel(teacher,
					task.getTTaskmodel().getId());
			return "detail";
		} else {
			return "input";
		}
	}

	/**
	 * 删除任务
	 * 
	 * @throws IOException
	 * 
	 */
	public void deleteTask() throws IOException {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		try {
			print_text_utf8(taskService.deleteTask(teacher, ids));
		} catch (Exception e) {
			e.printStackTrace();
			print_text_utf8("false");
		}
	}

	/**
	 * 进入分配任务页面
	 * 
	 */
	public String preAssign() {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		task = taskService.getTaskById(teacher, task.getId());
		if (task != null) {
			if (task.getCaseType() == 0) {
				PageConfig config = new PageConfig();
				config.setHql("from Student as st where st.tclass.classId in (select cm.tclass.classId from CourseManage as cm where cm.teacher.teacherId="+teacher.getTeacherId()+")");
				config.setCountHql("select count(*) from Student as st where st.tclass.classId in (select cm.tclass.classId from CourseManage as cm where cm.teacher.teacherId=" + teacher.getTeacherId() + ")");
				config.setPage(page);
				config.setPageSize(Constant.PAGE_SIZE);
				config.setUrl("task!preAssign.asp");
				config.setPerPageCount(Constant.PER_PAGE_COUNT);
				config.setExtendUrl("task.id=" + task.getId());
				studentList = studentService.getListByPage(config);
				classList = classService.getAllClass();
				return "preStudentAssign";
			} else if (task.getCaseType() == 1) {
				PageConfig config = new PageConfig();
				config.setHql("from TClass as t where t.classId in (select cm.tclass.classId from CourseManage as cm where cm.teacher.teacherId="+teacher.getTeacherId()+")");
				config.setCountHql("select count(*) from TClass as t where t.classId in (select cm.tclass.classId from CourseManage as cm where cm.teacher.teacherId="+teacher.getTeacherId()+")");
				config.setPage(page);
				config.setPageSize(Constant.PAGE_SIZE);
				config.setUrl("task_preAssign.asp");
				config.setExtendUrl("task.id=" + task.getId());
				config.setPerPageCount(Constant.PER_PAGE_COUNT);
				tclasses = classService.getListByPage(config);
				return "preClassAssign";
			}
		}
		return "input";
	}

	/**
	 * 多条件查询学生
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */

	public String searchStudent() throws UnsupportedEncodingException {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		task = taskService.getTaskById(teacher, task.getId());
		if (task != null) {
			if (!StringUtil.isEmpty(student.getStuName())) {
				student.setStuName(student.getStuName());
			}
			if (0 != student.getStuId()) {
				student.setStuId(student.getStuId());
			}
			if (!StringUtil.isEmpty(student.getStuIdc())) {
				student.setStuIdc(student.getStuIdc());
			}
			if (null != student.getStuSex() && !student.getStuSex().isEmpty()) {
				student.setStuSex(student.getStuSex());
			}
			if (null != student.getTclass()) {
				student.setTclass(student.getTclass());
			}
			String hql = studentService.creatHql(student, startTime, endTime);
			PageConfig config = new PageConfig();
			config.setHql(hql);
			config.setCountHql("select count(*) " + hql);
			config.setPage(page);
			config.setPageSize(Constant.PAGE_SIZE);
			config.setUrl("task_searchStudent.asp");
			config.setExtendUrl("student.studentName="
					+ URLEncoder.encode(URLEncoder.encode(student.getStuName(),
							Constant.UTF_8), Constant.UTF_8)
					+ "&student.sex=" + student.getStuSex() + "&startTime="
					+ startTime + "&endTime=" + endTime + "&student.classId="
					+ student.getTclass().getClassId() + "&student.studentNo="
					+ student.getStuId() + "&student.idcardNo="
					+ student.getStuIdc() + "&task.id=" + task.getId());
			config.setPerPageCount(Constant.PER_PAGE_COUNT);
			studentList = studentService.getListByPage(config);
			classList = classService.getAllClass();
			return "preStudentAssign";
		} else
			return "input";
	}

	/**
	 * 批量分配学生任务
	 * 
	 * @throws IOException
	 * 
	 */
	public void studentAssign() throws IOException {
		try {
			teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
			if (ids.length > 0 && null != task && task.getId() > 0) {
				taskService.addStudentAssign(ids, task.getId(), teacher);
				print_text_utf8("ok");
			} else {
				print_text_utf8("false");
			}
		} catch (Exception e) {
			e.printStackTrace();
			print_text_utf8("false");
		}
	}

	/**
	 * 多条件查询任务
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String searchTask() throws UnsupportedEncodingException {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		if (!StringUtil.isEmpty(task.getTaskName())) {
			task.setTaskName(
					URLDecoder.decode(task.getTaskName(), Constant.UTF_8));
		}
		String hql = taskService.creatTaskHql(task, startTime, endTime,
				teacher);

		PageConfig config = new PageConfig();
		config.setHql(hql);
		config.setCountHql("select count(*) " + hql);
		config.setPage(page);
		config.setPageSize(Constant.PAGE_SIZE);
		config.setUrl("task_searchTask.asp");
		config.setExtendUrl("task.taskName="
				+ URLEncoder.encode(
						URLEncoder.encode(task.getTaskName(), Constant.UTF_8),
						Constant.UTF_8)
				+ "&task.caseType=" + task.getCaseType() + "&startTime="
				+ startTime + "&endTime=" + endTime);
		config.setPerPageCount(Constant.PER_PAGE_COUNT);
		tasks = taskService.getListByPage(config);
		return "getTaskList";
	}

	/**
	 * 多条件查询班级
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String searchClass() throws UnsupportedEncodingException {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		task = taskService.getTaskById(teacher, task.getId());
		if (task != null) {
			if (!StringUtil.isEmpty(tclass.getClassName())) {
				tclass.setClassName(URLDecoder.decode(tclass.getClassName(),
						Constant.UTF_8));
			}
			String hql = classService.creatClassHql(tclass);
			PageConfig config = new PageConfig();
			config.setHql(hql);
			config.setCountHql("select count(*) " + hql);
			config.setPage(page);
			config.setPageSize(Constant.PAGE_SIZE);
			config.setUrl("task_searchClass.asp");
			config.setExtendUrl("tclass.className="
					+ URLEncoder.encode(URLEncoder.encode(tclass.getClassName(),
							Constant.UTF_8), Constant.UTF_8)
					+ "&tclass.ishaveTask=" + tclass.getIshaveTask()
					+ "&task.id=" + task.getId());
			config.setPerPageCount(Constant.PER_PAGE_COUNT);
			tclasses = classService.getListByPage(config);
			return "preClassAssign";
		} else
			return "input";
	}

	/**
	 * 班级任务分配
	 */
	public void classAssign() {
		try {
			teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
			if (classIds.length > 0 && null != task && task.getId() > 0) {
				taskService.addClassAssign(classIds, task.getId(), teacher);
				print_text_utf8("ok");
			} else {
				print_text_utf8("false");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 学生已分配任务的列表
	 *
	 * @return
	 * @throws UnsupportedEncodingException
	 */

	public String haveTaskList() throws UnsupportedEncodingException {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		PageConfig config = new PageConfig();
		config.setHql("from TStudentTask as model where model.TTask.teacher.id="
				+ teacher.getId());
		config.setCountHql(
				"select count(*) from TStudentTask as model where model.TTask.teacher.id="
						+ teacher.getId());
		config.setPage(page);
		config.setPageSize(Constant.PAGE_SIZE);
		config.setUrl("task_haveTaskList.asp");
		config.setPerPageCount(Constant.PER_PAGE_COUNT);
		stuTaskList = taskService.getStuTaskList(config);
		classList = classService.getAllClass();
		return "haveTaskList";
	}

	/**
	 * 多条件查询已分配任务
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String searchHaveTaskList() throws UnsupportedEncodingException {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		if (stuTask == null) {
			stuTask = new TStudentTask();
		}
		String hql = taskService.createHavaTaskHql(stuTask, teacher);
		String stuName = null == stuTask.getStudent() || StringUtil.isEmpty(stuTask.getStudent().getStuName()) ? ""
				: stuTask.getStudent().getStuName();
		String taskName = null == stuTask.getTTask() || StringUtil.isEmpty(stuTask.getTTask().getTaskName()) ? ""
				: stuTask.getTTask().getTaskName();
		int type = null != stuTask.getTTask() && stuTask.getTTask().getCaseType() > -1 ? stuTask.getTTask().getCaseType()
				: -1;
		String stuId = null == stuTask.getStudent() || null == stuTask.getStudent().getStuId() ? "" : stuTask.getStudent().getStuId()+"";
		PageConfig config = new PageConfig();
		config.setHql(hql);
		config.setCountHql("select count(*) " + hql);
		config.setPage(page);
		config.setPageSize(Constant.PAGE_SIZE);
		config.setUrl("task_searchHaveTaskList.asp");
		config.setPerPageCount(Constant.PER_PAGE_COUNT);
		config.setExtendUrl("stuTask.student.stuName="
				+ URLEncoder.encode(
						URLEncoder.encode(stuName,
								Constant.UTF_8),
						Constant.UTF_8)
				+ "&stuTask.TTask.taskName="
				+ URLEncoder.encode(URLEncoder.encode(
						taskName, Constant.UTF_8),
						Constant.UTF_8)
				+ "&stuTask.student.stuId="
				+ stuId + "&stuTask.TTask.caseType="
				+ type);
		stuTaskList = taskService.getStuTaskList(config);
		//classList = classService.getAllClass();
		return "haveTaskList";
	}

	/**
	 * 已分配的任务的详情
	 * 
	 * @return
	 */
	public String showHavaTaskDetail() {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		stuTask = taskService.getStuTaskById(ids[0], teacher);
		return "havaTaskDetail";
	}

	/**
	 * 批量删除已分配的任务
	 * 
	 * @throws IOException
	 * 
	 */
	public void deleteStuTask() throws IOException {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		try {
			taskService.deleteStuTask(teacher, ids);
			print_text_utf8("ok");
		} catch (Exception e) {
			e.printStackTrace();
			print_text_utf8("false");
		}
	}

	/**
	 * 显示已分配班级任务
	 * 
	 * @return
	 */
	public String showClassTasks() {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		PageConfig config = new PageConfig();
		config.setHql("from TClassTask as t where t.TTask.teacher.id="
				+ teacher.getId());
		config.setCountHql(
				"select count(*) from TClassTask as t where t.TTask.teacher.id="
						+ teacher.getId());
		config.setPage(page);
		config.setPageSize(Constant.PAGE_SIZE);
		config.setUrl("task_showClassTasks.asp");
		config.setPerPageCount(Constant.PER_PAGE_COUNT);
		tclasses = taskService.getClassTaskListByPage(config);
		return "showClassTasks";
	}

	/**
	 * 查询已分配任务
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String searchClassTask() throws UnsupportedEncodingException {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		if (!StringUtil.isEmpty(tclass.getClassName())) {
			tclass.setClassName(
					URLDecoder.decode(tclass.getClassName(), Constant.UTF_8));
		}
		if (!StringUtil.isEmpty(task.getTaskName())) {
			task.setTaskName(
					URLDecoder.decode(task.getTaskName(), Constant.UTF_8));
		}
		String hql = taskService.createClassTaskHql(tclass, task, teacher);
		PageConfig config = new PageConfig();
		config.setHql(hql);
		config.setCountHql("select count(*) " + hql);
		config.setPage(page);
		config.setPageSize(Constant.PAGE_SIZE);
		config.setUrl("task_searchClassTask.asp");
		config.setPerPageCount(Constant.PER_PAGE_COUNT);
		config.setExtendUrl("tclass.className="
				+ URLEncoder.encode(URLEncoder.encode(tclass.getClassName(),
						Constant.UTF_8), Constant.UTF_8)
				+ "&task.taskName="
				+ URLEncoder.encode(
						URLEncoder.encode(task.getTaskName(), Constant.UTF_8),
						Constant.UTF_8));
		tclasses = taskService.getClassTaskListByPage(config);
		return "showClassTasks";
	}

	/**
	 * 批量删除已分配的班级任务
	 * 
	 * @throws IOException
	 * 
	 */
	public void deleteClassTask() throws IOException {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		try {
			taskService.deleteClassTask(teacher, ids);
			print_text_utf8("ok");
		} catch (Exception e) {
			e.printStackTrace();
			print_text_utf8("false");
		}
	}

	/**
	 * 查看已分配班级任务学生成绩
	 * 
	 */
	public String getStuInfo() {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		stuTaskList = new ObjectData();
		stuTaskList.setList(taskService.getStudentTaskList(ids[0], teacher));
		return "studentScore";
	}

	/**
	 * 进入批量改分界面
	 * 
	 * @return
	 */
	public String preEditScores() {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		stuTaskList = new ObjectData();
		stuTaskList.setList(taskService.getStudentTaskList(ids[0], teacher));
		return "preEditScores";
	}

	/**
	 * 提交批量分数并修改成绩
	 * 
	 */
	public String editScores() {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		classTask = taskService.getClassTaskById(classTask.getId(), teacher);
		taskService.updateStuScores(classTask, ids);
		return "editScores";
	}

	/**
	 * 导出学生成绩
	 * 
	 * @throws IOException
	 * 
	 */
	public void exportScores() throws IOException {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		classTask = taskService.getClassTaskById(classTask.getId(), teacher);
		if (null != classTask) {
			HSSFWorkbook workbook = taskService.getStuScoresWorkbook(classTask);
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			workbook.write(os);
			ByteArrayInputStream in = new ByteArrayInputStream(
					os.toByteArray());
			String sheetName = "exportScores.xls";
			download(in, sheetName);
		}
	}

	public void downloadStuTask() {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		stuTask = taskService.getStuTaskById(stuTask.getId(), teacher);
		if (null != stuTask) {
			student = stuTask.getStudent();
			task = stuTask.getTTask();
			if (stuTask.getTTask().getCaseType() == 0) {
				downloadTask(stuTask.getTaskFile(), Constant.STUDENT_TASK + stuTask.getTTask().getTaskName() + "/" + student.getStuId());
			} else if (stuTask.getTTask().getCaseType() == 1) {
				downloadTask(stuTask.getTaskFile(),Constant.CLASS_TASK+ task.getTaskName() + "/" + student.getTclass().getClassId());
			}
		}
	}

	public void updateMark() throws IOException {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		Integer mark = stuTask.getMark();
		stuTask = taskService.getStuTaskById(stuTask.getId(), teacher);
		if (null != stuTask) {
			try {
				stuTask.setMark(mark);
				taskService.updateStuTask(stuTask);
				print_text_utf8("ok");
			} catch (Exception e) {
				print_text_utf8("false");
			}
		}
	}

	public String showMyTask() {
		student = (Student) getHttpSession().getAttribute(Constant.STUDENT);
		PageConfig config = new PageConfig();
		config.setHql("from TStudentTask as t where t.student.id=" + student.getId() + "order by t.TTask.timeLimit");
		config.setCountHql("select count(*) from TStudentTask as t where t.student.id=" + student.getId());
		config.setPage(page);
		config.setPageSize(Constant.PAGE_SIZE);
		config.setUrl("stuTask_showMyTask.asp");
		config.setPerPageCount(Constant.PER_PAGE_COUNT);
		stuTaskList = taskService.getStuTaskList(config);
		return "getTaskList";
	}

	public String searchMyTask() throws UnsupportedEncodingException {
		student = (Student) getHttpSession().getAttribute(Constant.STUDENT);
		if (!StringUtil.isEmpty(stuTask.getTTask().getTaskName())) {
			stuTask.getTTask().setTaskName(URLDecoder
					.decode(stuTask.getTTask().getTaskName(), Constant.UTF_8));
		}
		String hql = taskService.createMyTaskHql(stuTask, student);
		PageConfig config = new PageConfig();
		config.setHql(hql + " order by model.TTask.timeLimit");
		config.setCountHql("select count(*) " + hql);
		config.setPage(page);
		config.setPageSize(Constant.PAGE_SIZE);
		config.setUrl("stuTask_searchMyTask.asp");
		config.setPerPageCount(Constant.PER_PAGE_COUNT);
		config.setExtendUrl("stuTask.TTask.taskName="
				+ URLEncoder.encode(
						URLEncoder.encode(stuTask.getTTask().getTaskName(),
								Constant.UTF_8),
						Constant.UTF_8)
				+ "&stuTask.TTask.caseType=" + stuTask.getTTask().getCaseType()
				+ "&stuTask.isdemote=" + stuTask.getIsdemote());
		stuTaskList = taskService.getStuTaskList(config);
		return "getTaskList";
	}

	/**
	 * 查看任务进度或开始任务
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String showMyTaskDetail() throws UnsupportedEncodingException {
		student = (Student) getHttpSession().getAttribute(Constant.STUDENT);
		stuTask = taskService.getMyTask(ids[0], student);
		if (null != stuTask) {
			if (stuTask.getState() == 0) {
				stuTask.setState(1);
				taskService.updateMyTask(stuTask, student);
				return showMyTask();
			} else {
				if (stuTask.getTTask().getTimeLimit().getTime() < new Date().getTime()) {
					stuTask.setState(2); // 任务结束
					taskService.updateMyTask(stuTask, student);
				}
				return "showMyTaskDetail";
			}
		} else {
			return "input";
		}
	}

	/**
	 * 下载自己的任务模板
	 * 
	 */
	public void downloadMyModel() {
		student = (Student) getHttpSession().getAttribute(Constant.STUDENT);
		stuTask = taskService.getMyTask(stuTask.getId(), student);
		if (null != stuTask) {
			String modelFileName = stuTask.getTTask().getTTaskmodel()
					.getModelUrl();
			download(
					"taskModel" + modelFileName.substring(
							modelFileName.lastIndexOf("."),
							modelFileName.length()),
					Constant.TASKMODEL + modelFileName);
		}
	}

	private boolean checkLimit() {
		boolean flag = false;
		student = (Student) getHttpSession().getAttribute(Constant.STUDENT);
		stuTask = taskService.getMyTask(stuTask.getId(), student);
		tclass = student.getTclass();
		if (stuTask.getTTask().getTimeLimit().getTime() < new Date().getTime()) {
			flag = false;
		} else {
			flag = true;
		}

		return flag;
	}

	public String showMyUpload() {
		return "showMyUpload";
	}

	public void uploadMyWork() throws IOException {
		if (checkLimit()) {
			if (null != stuTask) {
				String fileName = student.getStuId() + student.getStuName() + ".rar";
				if (getUploadFileName().equals(fileName)) {
					String dir = "";
					File file = null;
					try {
						if (stuTask.getTTask().getCaseType() == 0) {
							dir = Constant.STUDENT_TASK + stuTask.getTTask().getTaskName() + "/" + student.getStuId();
						} else if (stuTask.getTTask().getCaseType() == 1) {
							dir = Constant.CLASS_TASK + stuTask.getTTask().getTaskName() + "/" + tclass.getClassId();
						} else {
							print_html_utf8("<script>parent.result('上传失败！系统错误，请联系老师！')</script>");
							return;
						}
						if (null != stuTask.getTaskFile() && !"".equals(stuTask.getTaskFile())) {
							fileName = stuTask.getTaskFile();
							file = new File(dir, fileName);
							if (file.isFile()) {
								file.delete();
							}
						} 
						upLoad(getUpload(), dir +"/"+ fileName);
						if (null == stuTask.getTaskFile() || !stuTask.getTaskFile().equals(getUploadFileName())) {
							stuTask.setTaskFile(getUploadFileName());
							taskService.updateMyTask(stuTask, student);
						}
						print_html_utf8("<script>parent.result('上传成功！')</script>");
					} catch (Exception e) {
						e.printStackTrace();
						file = new File(getRealPath(dir),this.getUploadFileName());
						if (file.isFile()) {
							file.delete();
						}
						print_html_utf8(
								"<script>parent.result('上传失败！')</script>");
					}
				} else {
					print_html_utf8(
							"<script>parent.result('上传失败！请更正为正确的文件名！');</script>");
				}
			} else {
				print_html_utf8("<script>parent.result('请操作自己的任务！');</script>");
			}
		} else {
			print_html_utf8("<script>parent.result('任务已过期！');</script>");
		}
	}

	/**
	 * 显示学生url修改界面
	 * 
	 */
	public String showMyUrl() {
		return "showMyUrl";
	}

	public void updateMyUrl() throws IOException {
		student = (Student) getHttpSession().getAttribute(Constant.STUDENT);
		String url = stuTask.getTaskUrl();
		stuTask = taskService.getMyTask(stuTask.getId(), student);
		if (null != stuTask) {
			stuTask.setTaskUrl(url);
			taskService.updateMyTask(stuTask, student);
			print_html_utf8("<script>parent.result('更新成功！');</script>");
		} else {
			print_html_utf8("<script>parent.result('请操作自己的任务！');</script>");
		}
	}

	/**
	 * 进入申请降级的页面
	 * 
	 * @return
	 */
	public String showMyDemote() {
		return "showMyDemote";
	}

	/**
	 * 申请降级
	 * 
	 * @throws IOException
	 * 
	 */
	public void updateMyDemote() throws IOException {
		student = (Student) getHttpSession().getAttribute(Constant.STUDENT);
		String demotionMessage = stuTask.getDemotionMessage();
		stuTask = taskService.getMyTask(stuTask.getId(), student);
		if (null != stuTask) {
			stuTask.setIsdemote(1);
			stuTask.setDemotionMessage(demotionMessage);
			taskService.updateMyTask(stuTask, student);
			print_html_utf8("<script>parent.result('申请降级成功！');</script>");
		} else {
			print_html_utf8("<script>parent.result('请操作自己的任务！');</script>");
		}
	}

	/**
	 * 下载我的降级模板
	 * 
	 */
	public void downloadMyDemoteModel() {
		student = (Student) getHttpSession().getAttribute(Constant.STUDENT);
		stuTask = taskService.getMyTask(stuTask.getId(), student);
		if (null != stuTask && stuTask.getIsdemote() == 1) {
			String modelFileName = stuTask.getTTask().getTTaskmodel().getDemoteModelUrl();
			download("DemoteModel" + modelFileName.substring(modelFileName.lastIndexOf("."),modelFileName.length()),Constant.TASKMODEL + modelFileName);
		}
	}

	/**
	 * 下载我的答案模板
	 * 
	 */
	public void downloadMyAnwserModel() {
		if (!checkLimit()) {
			String modelFileName = stuTask.getTTask().getTTaskmodel()
					.getAnwserModelUrl();
			download(
					"AnwserModel" + modelFileName.substring(
							modelFileName.lastIndexOf("."),
							modelFileName.length()),
					Constant.TASKMODEL + modelFileName);
		}
	}

	/**
	 * *************************************************************************
	 * ************** 以下为struts2封装数据**************************************
	 * *************************************************************************
	 */
	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public ObjectData getTasks() {
		return tasks;
	}

	public int[] getClassIds() {
		return classIds;
	}

	public void setClassIds(int[] classIds) {
		this.classIds = classIds;
	}

	public void setTasks(ObjectData tasks) {
		this.tasks = tasks;
	}

	public TTask getTask() {
		return task;
	}

	public void setTask(TTask task) {
		this.task = task;
	}

	public ObjectData getTaskModels() {
		return taskModels;
	}

	public void setTaskModels(ObjectData taskModels) {
		this.taskModels = taskModels;
	}

	public TTaskmodel getTaskModel() {
		return taskModel;
	}

	public void setTaskModel(TTaskmodel taskModel) {
		this.taskModel = taskModel;
	}

	public File getDemoteModelFile() {
		return demoteModelFile;
	}

	public void setDemoteModelFile(File demoteModelFile) {
		this.demoteModelFile = demoteModelFile;
	}

	public File getAnwserModelFile() {
		return anwserModelFile;
	}

	public void setAnwserModelFile(File anwserModelFile) {
		this.anwserModelFile = anwserModelFile;
	}

	public String getDemoteModelFileFileName() {
		return demoteModelFileFileName;
	}

	public void setDemoteModelFileFileName(String demoteModelFileFileName) {
		this.demoteModelFileFileName = demoteModelFileFileName;
	}

	public String getAnwserModelFileFileName() {
		return anwserModelFileFileName;
	}

	public void setAnwserModelFileFileName(String anwserModelFileFileName) {
		this.anwserModelFileFileName = anwserModelFileFileName;
	}

	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public Integer getTaskType() {
		return taskType;
	}

	public void setTaskType(Integer taskType) {
		this.taskType = taskType;
	}

	public ObjectData getTclasses() {
		return tclasses;
	}

	public void setTclasses(ObjectData tclasses) {
		this.tclasses = tclasses;
	}

	public TClass getTclass() {
		return tclass;
	}

	public void setTclass(TClass tclass) {
		this.tclass = tclass;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public ObjectData getStuTaskList() {
		return stuTaskList;
	}

	public void setStuTaskList(ObjectData stuTaskList) {
		this.stuTaskList = stuTaskList;
	}

	public TStudentTask getStuTask() {
		return stuTask;
	}

	public void setStuTask(TStudentTask stuTask) {
		this.stuTask = stuTask;
	}

	public List getClassList() {
		return classList;
	}

	public void setClassList(List classList) {
		this.classList = classList;
	}

	public ObjectData getStudentList() {
		return studentList;
	}

	public void setStudentList(ObjectData studentList) {
		this.studentList = studentList;
	}

	public TClassTask getClassTask() {
		return classTask;
	}

	public void setClassTask(TClassTask classTask) {
		this.classTask = classTask;
	}

}